package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This class is only used when the user wants to add a new HTTP source to a
 * Blog. It may seems that it would be better to made it a private class inside
 * the BlogController class since it is the only module using it. In order to
 * have a cleaner BlogController code we decided to split this code and made a
 * separated module.
 * 
 * This class is a Dialog that will ask the user for the information needed to
 * add a new HTTP source to a Blog (just the URL).
 * 
 * @author ole
 */
public class NewSourceDialog extends DustDialog {
	private Text text_entry;
	
	/*
	 * Private classes. Mostly classes to handle events of these dialogs.
	 */
	
	/**
	 * Class that will handle the selection of the subscription button of
	 * the dialog.
	 */
	private class SubscribeButtonListener extends SelectionAdapter {
		/**
		 * Get the URL typed in the text entry and dispose the dialog.
		 */
		@Override
		public void widgetSelected(SelectionEvent event) {
			result = text_entry.getText();
			doExit();
		}
	}
	
	/**
	 * Class that will handle the selection of the cancel button.
	 */
	private class CancelButtonListener extends SelectionAdapter {
		/**
		 * Close the dialog.
		 */
		@Override
		public void widgetSelected(SelectionEvent event) {
			shell.dispose();
		}
	}
	
	/*
	 * Public methods of the class AddSubscriptionDialog
	 */
	
	/**
	 * Class constructor.
	 */
	public NewSourceDialog(Shell parent) {
		super(parent);
		
		// This dialog has a label and a text entry where the user should type
		// the feed URL. Below that, two buttons, the subscribe button and the
		// cancel button.
		GridData grid;
		Composite toplevel_comp, label_text_comp, buttons_comp;
		Label label;
		Button subscribe_button, cancel_button;
		
		shell.setText("New source");
		
		// We need a top-level composite to store at the top the label and the
		// text entry and at the bottom the buttons.
		toplevel_comp = new Composite(shell, SWT.BORDER);
		toplevel_comp.setLayout(new GridLayout());
		grid = new GridData(SWT.TOP, SWT.LEFT, true, true);
		toplevel_comp.setLayoutData(grid);
		
		// A second-level composite to store the label and the text entry.
		label_text_comp = new Composite(toplevel_comp, SWT.NONE);
		label_text_comp.setLayout(new GridLayout(2, false));
		grid = new GridData(SWT.LEFT, SWT.TOP, true, true);
		label_text_comp.setLayoutData(grid);
		
		// Another second-level composite to store the buttons.
		buttons_comp = new Composite(toplevel_comp, SWT.NONE);
		buttons_comp.setLayout(new GridLayout(2, true));
		grid = new GridData(SWT.RIGHT, SWT.TOP, true, true);
		buttons_comp.setLayoutData(grid);
		
		// The label.
		label = new Label(label_text_comp, SWT.NONE);
		label.setText("New source URL:");
		
		// The text entry.
		this.text_entry = new Text(label_text_comp, SWT.BORDER);
		
		// Subscribe button.
		subscribe_button = new Button(buttons_comp, SWT.NONE);
		subscribe_button.setText("Add source");
		grid = new GridData(SWT.FILL, SWT.FILL, true, true);
		subscribe_button.setLayoutData(grid);
		subscribe_button.addSelectionListener(new SubscribeButtonListener());
		
		// Cancel button.
		cancel_button = new Button(buttons_comp, SWT.NONE);
		cancel_button.setText("Cancel");
		grid = new GridData(SWT.FILL, SWT.FILL, true, true);
		cancel_button.setLayoutData(grid);
		cancel_button.addSelectionListener(new CancelButtonListener());
	}
}
